#include "Renderer_PCH.h"
#include "RVertexArray.h"

/*--------------------------------------------------------------------------*/

RVertexArray::RVertexArray(const std::string& name) :
	m_bufferIB(0),
	m_name(name),
	m_numIndices(0)
{

}

/*--------------------------------------------------------------------------*/

RVertexArray::~RVertexArray()
{

}

/*--------------------------------------------------------------------------*/

const std::string& RVertexArray::GetName() const
{
	return m_name;
}

/*--------------------------------------------------------------------------*/

int RVertexArray::GetNumIndices() const
{
	return m_numIndices;
}

/*--------------------------------------------------------------------------*/

void RVertexArray::Bind(const RVertexArray* vao) const
{
	for (auto iter = m_declarations.begin(); iter != m_declarations.end(); ++iter)
	{
		if (iter->m_semanticInfo->m_semantic == ESemantic::iInstancing)
		{
			glBindBuffer(GL_ARRAY_BUFFER, iter->m_vertexBuffer);

			glEnableVertexAttribArray(iter->m_semanticInfo->m_location + 0);
			glVertexAttribPointer(	iter->m_semanticInfo->m_location + 0, 
									iter->m_semanticInfo->m_length, 
									iter->m_semanticInfo->m_type, 
									iter->m_semanticInfo->m_normalize, 
									64, 
									(void*)(0 + iter->m_offset));// + (uint)iter->m_dynamicSurface->vInstancing() - (uint)m_dynamicSurface->GetVertexData()));

			glEnableVertexAttribArray(iter->m_semanticInfo->m_location + 1);
			glVertexAttribPointer(	iter->m_semanticInfo->m_location + 1, 
									iter->m_semanticInfo->m_length, 
									iter->m_semanticInfo->m_type, 
									iter->m_semanticInfo->m_normalize, 
									64, 
									(void*)(16 + iter->m_offset));// + (uint)m_dynamicSurface->vInstancing() - (uint)m_dynamicSurface->GetVertexData()));

			glEnableVertexAttribArray(iter->m_semanticInfo->m_location + 2);
			glVertexAttribPointer(	iter->m_semanticInfo->m_location + 2, 
									iter->m_semanticInfo->m_length, 
									iter->m_semanticInfo->m_type, 
									iter->m_semanticInfo->m_normalize, 
									64, 
									(void*)(32 + iter->m_offset));// + (uint)m_dynamicSurface->vInstancing() - (uint)m_dynamicSurface->GetVertexData()));

			glEnableVertexAttribArray(iter->m_semanticInfo->m_location + 3);
			glVertexAttribPointer(	iter->m_semanticInfo->m_location + 3, 
									iter->m_semanticInfo->m_length, 
									iter->m_semanticInfo->m_type, 
									iter->m_semanticInfo->m_normalize, 
									64, 
									(void*)(48 + iter->m_offset));// + (uint)m_dynamicSurface->vInstancing() - (uint)m_dynamicSurface->GetVertexData()));

			
			glVertexAttribDivisor(	iter->m_semanticInfo->m_location + 0, 1);
			glVertexAttribDivisor(	iter->m_semanticInfo->m_location + 1, 1);
			glVertexAttribDivisor(	iter->m_semanticInfo->m_location + 2, 1);
			glVertexAttribDivisor(	iter->m_semanticInfo->m_location + 3, 1);
		}
		else
		{
			glBindBuffer(GL_ARRAY_BUFFER, iter->m_vertexBuffer);
			glEnableVertexAttribArray(iter->m_semanticInfo->m_location);
			glVertexAttribPointer(	iter->m_semanticInfo->m_location, 
									iter->m_semanticInfo->m_length, 
									iter->m_semanticInfo->m_type, 
									iter->m_semanticInfo->m_normalize, 
									0, 
									(void*) iter->m_offset);
			glVertexAttribDivisor(	iter->m_semanticInfo->m_location, 0);
		}
	}

	if (vao)
	{
		vao->Bind(NULL);
	}

	if (m_bufferIB)
	{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_bufferIB);
	}
}

/*--------------------------------------------------------------------------*/

void RVertexArray::Unbind()
{
	for (uint i = 0; i < 16; ++i)
	{
		glDisableVertexAttribArray(i);
		glVertexAttribDivisor(i, 0);
	}
}

/*--------------------------------------------------------------------------*/
